﻿#pragma once
#include <memory>
#include <string>
#include "circular_buffer.h"
#include <mutex>


template <class conn>
class db_conn_pool
{
protected:
	db_conn_pool() {}
	virtual ~db_conn_pool(){}

public:
	typedef conn connection;
	typedef std::shared_ptr<conn> connection_ptr;

public:
	virtual void init_connection() = 0;
	virtual connection_ptr get_connection_ptr() = 0;

	


protected:
	size_t pool_size;
	std::string _conn_str_addr;
	std::string _conn_str_user;
	std::string _conn_str_password;
	circular_buffer<connection_ptr> cir_buffer;
	std::mutex _mutex;

protected:
	virtual connection_ptr create_connection() = 0;

protected:
	db_conn_pool(size_t size, std::string con_str)
		:pool_size(size),
		_conn_str(con_str)
	{

	}
};




template<class conn_pool,class conn>
class connection_wrap
{
public:
	connection_wrap(conn_pool& conn_pools, std::shared_ptr<conn> conn_ptr)
		:pool(conn_pools),
		con_ptr(conn_ptr)
	{

	}

	~connection_wrap()
	{
		pool.release_connection(con_ptr);
	}

private:
	conn_pool& pool;
	std::shared_ptr<conn> con_ptr;
};